<?php
/**
 * 全局控制器页面的父类
 * 
 * 实现一些全局的页面显示
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */
class general extends spController
{
    /**
     * 默认风格
     */
    var $themes = "default";

    /**
     * 购物车内容
     */
    var $cartarr;
    /**
     * 当前用户内容
     */
    var $userarr;
    /**
     * 所有国家内容
     */
    var $countrarr;
    /**
     * 当前货币代码
     */
    var $code2;
    /**
     * 当前购物车统计
     */
    var $total;
    /**
     * 订单状态
     */
    var $orders_status;
    /**
     * 所有货币汇率
     */
    var $currencyarr;
    /**
     * 快递运输
     */
    var $shippingarr;
    /**
     * 返回商品分类到程序用
     */
    var $categoriesarray;
    /**
     *  返回信息分类到程序用
     */
    var $newscategories;

    var $facebook;

    /**
     * 覆盖控制器构造函数，进行相关的赋值操作
     */
    public function __construct()
    {
        parent::__construct();
        $this->setLang('en'); //默认语言

        //获取登入状态
        $customers = spClass('customersModel')->online();
        $this->user = $customers; //给模板中使用
        $this->userarr = $customers; //给程序中使用
        if (FLOGIN == 'True') {//提示注册记录浏览次数    
            if (isset($_COOKIE['FLogin'])) {
                setcookie('FLogin', $_COOKIE['FLogin'] + 1, time() + 1440, WEBDIR);
            } else {
                setcookie('FLogin', 1, time() + 1440, WEBDIR);
            }
        }
        if (FACEBOOK_TYPE) {
            import("facebook/facebook.php");
            $facebook = new Facebook(array(
                'appId' => FACEBOOK_APPID,
                'secret' => FACEBOOK_SECRET,
                ));
            $this->facebook = $facebook;
            $this->logoutUrl = $this->facebook->getLogoutUrl();
            $loginUrl = $this->facebook->getLoginUrl(array(
                'canvas' => 1,
                'fbconnect' => 0,
                'scope' => 'user_about_me,email',
                'redirect_uri' => WEBURL . WEBDIR . 'facebook.html', //the url to go to after a successful login
                ));
            $this->loginUrl = $loginUrl;
        }

        //判断当前货币选择
        if ($this->spArgs('editcurrency') == 1) //如果选择货币了
            {
            $_SESSION['currencies_id'] = $this->spArgs("currency", (int)DEFAULT_CURRENCY);
        }
        if ($_SESSION['currencies_id'] == "") //当前记录为空就给预默认货币
            {
            $_SESSION['currencies_id'] = (int)DEFAULT_CURRENCY;
        }

        //为获取隐藏表单做准备
        $hidden_get_variables = '';
        $hidden_get_order = '';
        //$get=reset($_GET);
        $get = $this->spArgs();
        //货币汇率选择隐藏表单
        while (list($key, $value) = each($get)) {
            if (($key != 'currency') && ($key != 'editcurrency') && ($key != 'c') && ($key !=
                'a') && ($key != 'x') && ($key != 'y') && ($key != 'alias') && ($key != 'order')) {
                $hidden_get_variables[$key] = $value;
                $this->hidden_get_variables = $hidden_get_variables;
            }
        }

        //获取商品的全部分类
        $Carry = spClass('categoriesModel')->spCache(3600 * 24)->Categories_son(0);
        $this->categoriesarray = $Carry;
        $this->categoriesTop = $Carry;
        //dump($Carry);
        //获取信息分类
        $this->newscategories = $newscategories = spClass('newscategoriesModel')->
            spCache(3600 * 24)->Categories_son(2); //分类
        $this->newscategoriesright = $newscategories;

        //购物车
        $this->cartarr = spClass("CartAPI"); //
        $this->iscart = $this->cartarr->checkCart(); //检查购物车给模板中使用
        $this->count = $count = $this->cartarr->CountPrice(); //获取购物车统计,给模板中使用
        $this->total = $count; //将统计给程序中使用
        //dump($count);
        //获取当前和来源URL
        $_SESSION['request_uri'] = request_uri();
        //echo $_SERVER['HTTP_REFERER'];
        $this->URL = $_SERVER['REQUEST_URI']; //获取当前网址

        //获取全部汇率
        $currency = spClass('currenciesModel')->Currencies_List();
        $this->currency = $currency; //汇率清单
        $this->currencyarr = $currency; //汇率给程序使用

        $temp_currency_arr = spClass('currenciesModel')->Get_currency($_SESSION['currencies_id']);

        $this->symbol_left = $temp_currency_arr['symbol_left']; //当前汇率符号 ￥$
        $this->value = $temp_currency_arr['value']; //当前汇率0.63
        $this->code = $temp_currency_arr['code']; //当前汇率符号CNY
        $this->code2 = $temp_currency_arr;
        //dump($currency);
        //获取全部国家
        $countries = spClass('countriesModel')->spCache(3600 * 24)->Countries_List(); //所有国家

        $this->countries = $countries;
        $this->countrarr = $countries; //国家给程序使用
        //获取全部购物状态
        $this->orders_status = spClass('orders_statusModel')->spCache(3600 * 24)->
            findAll(null);


        $KD = trim(KD);
        $arr = explode(chr(13), $KD);
        foreach ($arr as $k => $v) {
            $shippingarr[$k] = explode('/', trim($v));
        }
        $this->shippinglist = $shippingarr; //给前台用
        $this->shippingarr = $shippingarr; //给程序用
        
        $this->footother = stripslashes(FOOTOTHER); //底部脚本-后台
        $this->footother2 = stripslashes(FOOTOTHER2); //统计代码-后台
        $this->typearr = T('Linktype');
    }

    /**
     * 操作记录存储
     * 
     */
    function paypallog($loginfo)
    {
        $loginfo = $this->userarr['customers_email_address'].' '.$loginfo . "\r\n";
        //记录日志
        $handle = fopen("log/paypalEC" . Date('Ymd') . ".log", 'a+');
        fwrite($handle, Date('Y-m-d H:i:s') . ' ' . $loginfo);
        fclose($handle);
    }
    
    /**
     * 这个看成函数重载也无妨，功能就是查找$haystack是不是在$needle中存在
     * @para string $haystack
     * @para string $needle
     *
     * @return boolean
     */

    function strexists($haystack, $needle)
    {
        return !(strpos($haystack, $needle) === false);
    }
    /**
     * 根据ID返回国家代码
     * @name $entry_country_id 国家ID
     * @name $x 默认返回国家全称 2返回双字母名称 3返回3字母名称 4返回ID
     * @name $y 默认是1以ID匹配 2以双字母编号 3是以3字母匹配,4是以全称匹配
     */
    public function forcount($entry_country, $x = 1, $y = 1)
    {
        switch ($y) {
            case 1:
                $str = 'countries_id';
                break;
            case 2:
                $str = 'countries_iso_code_2';
                break;
            case 3:
                $str = 'countries_iso_code_3';
                break;
            case 4:
                $str = 'countries_name';
                break;
            default:
                $str = 'countries_id';
        }
        foreach ($this->countrarr as $k => $v) {
            //dump($k);
            //echo $v['countries_id'];
            if ($v[$str] == $entry_country) {
                //echo "aaa";
                switch ($x) {
                    case 1:
                        $str = $v['countries_name'];
                        break;
                    case 2:
                        $str = $v['countries_iso_code_2'];
                        break;
                    case 3:
                        $str = $v['countries_iso_code_3'];
                        break;
                    case 4:
                        $str = $v['countries_id'];
                        break;
                    default:
                        $str = $v['countries_name'];
                }
                return $str;
            }
            //echo $str;

        }
        return false;
    }
    /**
     * 根据编号返回所属汇率数组
     * @name $currencystr 匹配字符串
     * @name $x 默认1 以ID匹配 2以双字代码匹配 3以货币符号
     * 
     */
    public function return_currency($currencystr, $x = 1)
    {
        //dump($this->currencyarr);
        switch ($x) {
            case 1:
                $str = 'currencies_id';
                break;
            case 2:
                $str = 'code';
                break;
            case 3:
                $str = 'symbol_left';
                break;
            default:
                $str = 'currencies_id';
        }
        foreach ($this->currencyarr as $k => $v) {
            if ($v[$str] == $currencystr) {
                return $v;
            }
        }

    }


    /**
     * 评论过滤函数
     * @param $string   提交过来的字符串
     * @param $replace  过滤后的字符
     * 
     * @param 过滤后返回字符串$str
     */
    public function plreplace($string, $replace)
    {
        $plstr = explode('||', PLSTR);
        foreach ($plstr as $k => $v) {
            $str = str_replace($plstr, $replace, $string);
        }

        return $str;
    }


    /**
     * 多维数组的搜索
     * @param $parents   被搜数组
     * @param $searched  要搜的数组
     * 
     * @param 存在返回键值，不存在返回false
     */
    public function multidimensional_search($parents, $searched)
    {
        if (empty($searched) || empty($parents)) {
            return false;
        }

        foreach ($parents as $key => $value) {
            $exists = true;
            foreach ($searched as $skey => $svalue) {
                $exists = ($exists && isset($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
            }
            if ($exists) {
                return $key;
            }
        }

        return false;
    }

}

?>